<template>
	<div>
		<div>
			<el-input style="width: 300px; margin-right: 10px; margin-left: -500px;" clearable @clear="initDistr"
				v-model="patientId" @keydown.enter.native="initDistr" placeholder="请输入病人编号进行搜索"
				prefix-icon="el-icon-search">
			</el-input>
			<el-button type="primary" @click="initDistr">搜索</el-button>
			<el-button type="primary" @click="showDistrAdd">添加</el-button>
		</div>
		<div>
			<el-table :data="DistirbuteData" stripe border style="width: 100%">
				<el-table-column prop="patientId" label="病人编号" width="100">
				</el-table-column>
				<el-table-column prop="foodId" label="食物编号" width="100">
				</el-table-column>
				<el-table-column prop="supplyDay" label="供应时间" width="150">

				</el-table-column>
				<el-table-column prop="supplyWeekday" label="供应周天" width="100">
				</el-table-column>
				<el-table-column label="操作" align="center" width="200">
					<template slot-scope="scope">
						<el-button @click="showDistrEdit(scope.row)">编辑</el-button>
						<el-button @click="deletePlan(scope.row)" type="danger">删除</el-button>
					</template>
				</el-table-column>
			</el-table>
			<el-pagination background @current-change="currentChange" layout="prev, pager, next,jumper,->,total"
				:total=total>
			</el-pagination>
		</div>
		<el-dialog title="服务管理对象添加" :visible.sync="dialogVisible">
			<DistributeAdd :dialogVisible.sync="dialogVisible" @add="initDistr"></DistributeAdd>
		</el-dialog>

		<el-dialog title="服务管理对象编辑" :visible.sync="editVisible">
			<DistributeEdit :editVisible.sync="editVisible" :formData="formData"></DistributeEdit>
		</el-dialog>
	</div>
</template>

<script>
	import DistributeAdd from './distribute_add.vue'
	import DistributeEdit from './distribute_edit.vue'

	export default {
		name: "Distribute",
		components: {
			DistributeAdd,
			DistributeEdit
		},
		data() {
			return {
				DistirbuteData: [],
				total: 0,
				currentPage: 1,
				size: 10,
				patientId: '',
				dialogVisible: false,
				editVisible: false,
				formData: {
					patientId: '',
					foodId: '',
					supplyDay: '',
					supplyWeekday: ''
				}
			}
		},
		mounted() {
			this.initDistr();
		},
		methods: {
			showDistrEdit(data) {
				this.editVisible = true;
				this.formData = data;
			},
			deletePlan(data) {
				this.$confirm('此操作将永久删除' + data.patientId + '号病人的康复计划，是否继续？', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(() => {
					this.getRequest('/distribute/delete?patientId=' + data.patientId).then(resp => {
						if (resp) {
							this.initDistr();
						}
					})
				}).catch(() => {
					this.$message({
						type: 'info',
						message: '已取消删除'
					});
				});
			},
			currentChange(currentPage) {
				this.currentPage = currentPage;
				this.initDistr();
			},
			showDistrAdd() {
				this.dialogVisible = true
			},
			initDistr() {
				this.getRequest('/distribute/planList?currentPage=' + this.currentPage + '&size=' + this.size +
					'&patientId=' + this.patientId).then(resp => {
					if (resp) {
						this.DistirbuteData = resp.data;
						this.total = resp.total;
					}
				})
			}
		}
	}
</script>

<style>
</style>
